/*
 *  $Id: Sacrifice.cs 1068 2010-05-21 14:03:07Z thenn.erannor $
 *	This file is a part of Ragima CCG.
 *	(C) 2008-2009 Ragima development group.
 *
 *	Licensed under the terms of the GNU GPL License version 2.
 *
 */

using System.Collections.Generic;

namespace Ragima.Cards001.Light.Spells {
    public class Sacrifice: SpellCard<ChainData> {
        private Languages.Phrase TargetError;

        public Sacrifice() : base("001_l_s_sacrifice") {
            Level = CompleteLevel.ReadyForTest;
            Version = 0;
            Element = ElementType.Light;
            Target = new ChainTarget(NotSameCreatures,
                new CreatureTarget(GetTargetPhrase(), true, false),
                CreatureTarget.Both);
            TargetError = GetNewPhrase("target.error");
            AddManaCost(1);
        }

        private bool NotSameCreatures(TargetArgs<ChainData> args) {
            TargetArgs<Creature> ca1, ca2;
            if (!args.Target.GetArgs(out ca1, out ca2))
                return false;
            if (ca1.Target == ca2.Target) {
                args.Player.Game.ErrorNotice(TargetError.GetText());
                return false;
            }
            return true;
        }

        private static bool NeedDispell(Affect affect) {
            return affect.Dispellable && affect.Element != ElementType.Light;
        }

        protected override void DoAction(TargetArgs<ChainData> args) {
            TargetArgs<Creature> ca1, ca2;
            if (!args.Target.GetArgs(out ca1, out ca2))
                return;
            List<Affect> dispelled = new List<Affect>(ca2.Target.GetAffects(false, NeedDispell));
            if (dispelled.Count == 0)
                return;
            ca1.Target.AddAffect(new SacrificeAffect(dispelled.Count));
            foreach (Affect affect in dispelled)
                affect.Dispell();
        }

        private class SacrificeAffect : SpellAffect<Sacrifice> {
            private TimeAffect _timer;

            public SacrificeAffect(int count) : base(true) {
                DefaultDescription = true;
                _timer = new TimeAffect(count * 4, new Affects.ModMaxHealth(-3));
                Add(_timer);
            }

            public override string DescriptionText {
                get { return Description.GetText(_timer.Phases/4); }
            }
        }
    }
}
